package cn.jcet.mapreduce.weblog;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/17 23:17
 */
public class LogMapper extends Mapper<LongWritable,Text,Text,NullWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String line = value.toString();

        // 抽取清洗具体逻辑
        boolean result = paseLog(line,context);

        //  如果不成立，直接结束
        if (!result){
            return;
        }
        // 写出
        context.write(value,NullWritable.get());
    }

    private boolean paseLog(String line, Context context) {
        // 进行切割
        String[] split = line.split(" ");

        if (split.length >11){
            context.getCounter("map","true").increment(1);
            return true;
        }else {
            context.getCounter("map","false").increment(1);
            return false;
        }
    }
}
